package com.manager.cms.controller;

import com.manager.cms.annotation.IgnoreAuth;
import com.manager.cms.annotation.IgnoreLogin;
import com.manager.cms.bo.SysAdmin;
import com.manager.cms.bo.UserInfo;
import com.manager.cms.bo.UserRole;
import com.manager.cms.common.CommonResponse;
import com.manager.cms.common.Pageable;
import com.manager.cms.service.UserInfoService;
import com.manager.cms.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 
 *
 * @author 
 * @version v1.0
 * @date 2020-05-19
 */
@Slf4j
@RestController
@RequestMapping(value = "/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userinfoservice;

    @Autowired
    private UserRoleService userroleservice;

    @IgnoreAuth
    @IgnoreLogin
    @PostMapping("/register")
    public CommonResponse register(HttpServletRequest request, HttpServletResponse response, UserInfo record) {
        try {
            UserInfo query = new UserInfo();
            query.setUsername(record.getUsername());
            List<UserInfo> list = userinfoservice.getList(query);
            if (list.size() > 0) {
                return CommonResponse.fail("账号已存在");
            }
            record.setId(UUID.randomUUID().toString());
            record.setModifyDate(new Date());
            record.setCreateDate(new Date());
            userinfoservice.insert(record);

            UserRole userRole = new UserRole();
            userRole.setId(UUID.randomUUID().toString());
            userRole.setUserId(record.getId());
            userRole.setRole("user");
            userroleservice.insert(userRole);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @PostMapping("/save")
    public CommonResponse save(HttpServletRequest request, HttpServletResponse response, UserInfo record) {
        try {
            if (record.getId()==null||record.getId().length() == 0) {
                UserInfo query = new UserInfo();
                query.setUsername(record.getUsername());
                List<UserInfo> list = userinfoservice.getList(query);
                if (list.size() > 0) {
                    return CommonResponse.fail("账号已存在");
                }
                record.setId(UUID.randomUUID().toString());
                record.setCreateDate(new Date());
                record.setModifyDate(new Date());
                userinfoservice.insert(record);

                UserRole userRole = new UserRole();
                userRole.setId(UUID.randomUUID().toString());
                userRole.setUserId(record.getId());
                userRole.setRole("user");
                userroleservice.insert(userRole);
            } else {
                UserInfo userInfo = userinfoservice.selectByPrimaryKey(record.getId());
                record.setCreateDate(userInfo.getCreateDate());
                record.setModifyDate(new Date());
                userinfoservice.update(record);
            }
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @PostMapping("/delete")
    public CommonResponse del(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            userinfoservice.del(id);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("删除失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/selectByPrimaryKey")
    public CommonResponse selectByPrimaryKey(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            UserInfo info = userinfoservice.selectByPrimaryKey(id);
            return CommonResponse.succeed(info);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/list")
    public CommonResponse getList(HttpServletRequest request, HttpServletResponse response, UserInfo record) {
        try {
            List<UserInfo> list = userinfoservice.getList(record);
            return CommonResponse.succeed(list);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/pageList")
    public CommonResponse pageList(HttpServletRequest request, HttpServletResponse response, UserInfo record,
                                   @RequestParam(value="page",defaultValue = "1")Integer page,
                                   @RequestParam(value="limit",defaultValue = "20")Integer limit) {
        try {
            if(StringUtils.isEmpty(record.getMobile())){
                record.setMobile(null);
            }
            if(StringUtils.isEmpty(record.getName())){
                record.setName(null);
            }
            if(StringUtils.isEmpty(record.getUsername())){
                record.setUsername(null);
            }

            Pageable<UserInfo> pageable = userinfoservice.pageList(record,page,limit);
            return CommonResponse.succeed(pageable);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }
}